package com.ccp.dev.im.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.im.dao.ImRoomMessageDao;
import com.ccp.dev.im.dao.ImRoomMsgStatusDao;
import com.ccp.dev.im.model.ImRoomMessage;
import com.ccp.dev.im.model.ImRoomMsgStatus;
import com.ccp.dev.im.model.ImUser;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * 群聊消息 信息操作服务类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ImRoomMessageService extends BaseService<ImRoomMessage> {

    @Resource
    private ImRoomMessageDao dao;
    @Resource
    private ImRoomMsgStatusService imRoomMsgStatusService;
    @Resource
    private ImRoomMsgStatusDao imRoomMsgStatusDao;
    @Resource
    private ImRoomUserService imRoomUserService;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<ImRoomMessage> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (ImRoomMessage data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    /**
     * 根据收用户id查询离线消息
     * @param userId  用户id
     * @return  list
     */
    public List<ImRoomMessage> queryOfflineMsg(String userId){
        if(StringUtils.isNotEmpty(userId)){
            Map<String,Object> map = new HashMap(1);
            map.put("userId",userId);
            return dao.queryUnReadMsgByUserId(map);
        }
        return new ArrayList<>();
    }

    /**
     * 重写add 添加消息状态
     * @param imRoomMessage model
     * @return int
     */
    @Override
    public int add(ImRoomMessage imRoomMessage){
        String toRoom = imRoomMessage.getToRoom();
        String fromId = imRoomMessage.getFromId();
        Set<String> userIdSet =  imRoomUserService.getRoomUserIdsByRoomId(toRoom);
        List<ImRoomMsgStatus> imRoomMsgStatusList = new ArrayList<>();
        for(String id : userIdSet){
            if(id.equals(fromId)){
                continue;
            }
            ImRoomMsgStatus imRoomMsgStatus = imRoomMsgStatusService.getByRoomIdAndUserId(toRoom,id);
            if(BeanUtils.isEmpty(imRoomMsgStatus)){
                ImRoomMsgStatus irms = new ImRoomMsgStatus();
                irms.setRoomId(toRoom);
                irms.setUserId(id);
                irms.setUnreadTime(imRoomMessage.getSendTime());
                imRoomMsgStatusList.add(irms);
            }
        }
        if(!imRoomMsgStatusList.isEmpty()){
            imRoomMsgStatusDao.addList(imRoomMsgStatusList);
        }
        return dao.add(imRoomMessage);
    }
    /**
     * 根据群组id查询聊天消息
     * @param queryFilter  queryFilter
     * @return  list
     */
    public List<Map<String,Object>> queryMsgByRoomId(QueryFilter queryFilter){
        return dao.queryMsgByRoomId(queryFilter);
    }

    /**
     * 某个群聊里面删除所有聊天记录
     *
     * @param roomId  群ID
     * @return 删除条数
     */
    @Transactional(rollbackFor = Exception.class)
    public int delByRoomId(String roomId) {
        return dao.delByRoomId(roomId);
    }
}
